package com.cst.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cst.reggie.common.CustomException;
import com.cst.reggie.entity.Category;
import com.cst.reggie.entity.Dish;
import com.cst.reggie.entity.Setmeal;
import com.cst.reggie.mapper.CategoryMapper;
import com.cst.reggie.service.CategoryService;
import com.cst.reggie.service.DishService;
import com.cst.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : CST
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setMealService;

    /**
     * 根据id删除分类，删除前进行判断
     * @param ids
     */
    @Override
    public void remove(Long ids) {

        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.eq(Dish::getCategoryId,ids);
        long count1 = dishService.count(dishQueryWrapper);
        // 查询分类是否关联菜品
        if (count1 > 0){
            throw new CustomException("分类已经关联菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        long count2 = setMealService.count(setMealLambdaQueryWrapper);
        // 查询分类是否关联套餐
        if (count2 > 0){
            // 返回业务异常
            throw new CustomException("分类已经关联套餐，不能删除");
        }
        //正常删除
        super.removeById(ids);
    }
}
